package com.example.evalex;

import com.udojava.evalex.Expression;
import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;

/**
 * Created at 2017/11/6 18:34
 *
 * @author gongweixin
 */
public class Computer {

    @Test(expected = java.lang.NumberFormatException.class)
    public void testToNumberFormatException() {
        Expression expression = new Expression("(M8026-M8023)/M8026");
        expression.with("M1", new BigDecimal(1.0));
        expression.with("M8026", new BigDecimal(6056.0));
        expression.eval();
    }

    @Test(expected = java.lang.RuntimeException.class)
    public void testToException() {
        Expression expression = new Expression("(M8026-M8023)/M8026");
        expression.with("M1", new BigDecimal(1.0));
        expression.eval();
    }

    @Test(expected = java.util.EmptyStackException.class)
    public void testToString() {
        Expression expression = new Expression("(M8023/M9359)*3600");
        expression.with("M9359", new BigDecimal(1.3740356495E10));
        expression.with("M1", new BigDecimal(1306639.0));
        expression.eval();
    }

    @Test
    public void 分子分母都是零() {
        try {
            Expression expression = new Expression("M8023/M9359");
            expression.with("M9359", new BigDecimal(0));
            expression.with("M8023", new BigDecimal(0));
            expression.eval();
            throw new RuntimeException("");
        } catch (java.lang.ArithmeticException e) {
            Assert.assertEquals(e.getMessage(), "Division undefined");
        }
    }

    @Test
    public void 分母是零() {
        try {
            Expression expression = new Expression("M8023/M9359");
            expression.with("M9359", new BigDecimal(0));
            expression.with("M8023", new BigDecimal(1));
            expression.eval();
            throw new RuntimeException("");
        } catch (java.lang.ArithmeticException e) {
            Assert.assertEquals(e.getMessage(), "Division by zero");
        }
    }

    @Test(expected = java.lang.NumberFormatException.class)
    public void 带符号表达式() {
        Expression expression = new Expression("(M8023/M9359)+M8060");
        expression.with("(M8023/M9359)", new BigDecimal(0.5));
        expression.with("M8060", new BigDecimal(1));
        expression.eval();
    }
}
